const {default :axios} = require("axios");
const jwt = require("jsonwebtoken")
var template = require('art-template');

const login=(req, res)=>{
    const {username, password}=req.body;
    console.log(username==0);
    if (username==0||password==0) {
        // return res.status(400).render('admin/error',{msg:"请完善信息"})
        res.render('admin/login',{username:username,password:password,msg:"请完善信息😅"})
    }else{
        axios({
            url:"http://1.117.92.175:8081/user/login",
            method:"POST",
            params: {
                username:username,
                password :password,
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data=>{
           if(data.data.code==200){
                    let flag=false
                    if((username==jwt.decode(data.data.object).username)==false){
                        flag=true;
                        res.render('admin/login',{username:username,password:password,msg:"账号出错，注意区分大小写😅"})
                    }
                    
                    if(flag==false){
                        console.log(data.data.object);
                        console.log(jwt.decode(data.data.object));
                        req.session.token=data.data.object;
                        // req.app.locals.users[req.app.locals.users.length]=jwt.decode(data.data.object).id
                        // res.render('admin/index')
                        // console.log("31:"+req.app.locals.users);
                    }
           }else{
                res.render('admin/login',{username:username,password:password,msg:"用户名或密码错误"})
           }
        })
        .catch(err=>{
            console.log(err);
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
    
}
module.exports=login;